<template>
	<view class="container">
		<!-- <u-navbar bgColor="#0065FF" titleStyle="color:#fff;" leftIconColor="#fff" title="公司管理" autoBack placeholder></u-navbar> -->
		<view class="content">
			<view class="item">
				<text class="row">公司全名</text>
				<text>{{ userInfo.dept.deptName }}</text>
			</view>
			<view class="item">
				<text class="row">统一信用社会代码</text>
				<text>{{ userInfo.dept.code }}</text>
			</view>
			<view class="item">
				<text class="column">营业执照</text>
				<!-- {{$tool.oss(userInfo.dept.dept.license + '1')}} -->
				<u--image :src="$tool.oss(userInfo.dept.license)" width="80px" height="80px"
					@click="preview([userInfo.dept.license], 0)"></u--image>
			</view>
			<view class="item">
				<text class="column">食品经营许可证</text>
				<u--image :src="$tool.oss(userInfo.dept.food)" width="80px" height="80px"
					@click="preview([userInfo.dept.food], 0)"></u--image>
			</view>
			<view class="item" style="border: none;">
				<text class="column">国家企业信用信息公示系统查询结果截图</text>
				<u--image :src="$tool.oss(userInfo.dept.iso9001)" width="80px" height="80px"
					@click="preview([userInfo.dept.iso9001], 0)"></u--image>
			</view>
			<!-- <view class="item" style="border: none;">
				<text class="column">环境管理体系（ISO14001）认证证书</text>
				<u--image :src="baseUrl + userInfo.dept.iso14001" width="80px" height="80px" @click="preview([userInfo.dept.food], 0)"></u--image>
			</view>
			<view class="item">
				<text class="column">HACCP体系认证证书</text>
				<u--image :src="baseUrl + userInfo.dept.haccp" width="80px" height="80px" @click="preview([userInfo.dept.food], 0)"></u--image>
			</view> -->

			<!-- <view class="item">
				<text class="column">CNAS检测报告</text>
				<u--image :src="baseUrl + userInfo.dept.cnas" width="80px" height="80px" @click="preview([userInfo.dept.cnas], 0)"></u--image>
			</view>
			<view class="item">
				<text class="column">资质</text>
				<u-upload :deletable="false" :maxSize="10485760" @oversize="oversize" :fileList="userInfo.dept.others" @afterRead="afterRead" multiple></u-upload>
			</view> -->
			<view class="item">
				<text class="column">法人身份证照片</text>
				<view class="imgs">
					<view class="imgs_item">
						<text>正面</text>
						<u--image :src="$tool.oss(userInfo.dept.idCardTop)" width="80px" height="80px"
							@click="preview([userInfo.dept.id_card_top, userInfo.dept.id_card_back], 0)"></u--image>
					</view>
					<view class="imgs_item">
						<text>反面</text>
						<u--image :src="$tool.oss(userInfo.dept.idCardTop)" width="80px" height="80px"
							@click="preview([userInfo.dept.id_card_top, userInfo.dept.id_card_back], 1)"></u--image>
					</view>
				</view>
			</view>
			<view class="item">
				<text class="column">食品检测员（高级）</text>
				<u--image :src="$tool.oss(userInfo.dept.foodInspector)" width="80px" height="80px"
					@click="preview([userInfo.dept.food_inspector], 0)"></u--image>
			</view>
			<view class="item">
				<text class="column">经营人员健康证</text>
				<view class="imgs">
					<u--image v-for="(item, index) in userInfo.dept.health.split(',')" :src="$tool.oss(item)"
						width="80px" height="80px" @click="preview(userInfo.dept.health.split(','), index)"></u--image>
				</view>
			</view>
			<view class="item">
				<text class="column">业绩证明</text>
				<view class="imgs">
					<u--image v-for="(item, index) in userInfo.dept.performance.split(',')" :src="$tool.oss(item)"
						width="80px" height="80px"
						@click="preview(userInfo.dept.performance.split(','), index)"></u--image>
				</view>
			</view>
			<view class="item">
				<text class="column">所属类目</text>
				<view class="tags">
					<u-tag v-for="(item, index) in userInfo.dept.cateList" :text="item.name" plain size="mini"
						color="#0065FF"></u-tag>
					<u-button @click="cateShow = true" iconColor="#fff" color="#0065FF" icon="plus"></u-button>
				</view>
			</view>
			<view class="item">
				<text class="row">公司性质</text>
				<text>{{ userInfo.dept.nature }}</text>
			</view>
			<view class="item">
				<text class="row">纳税人资质</text>
				<text>{{ userInfo.dept.aptitude }}</text>
			</view>
			<view class="item">
				<text class="row">联系人姓名</text>
				<text>{{ userInfo.dept.leader }}</text>
			</view>
			<view class="item">
				<text class="row">手机号码</text>
				<text>{{ userInfo.dept.phone }}</text>
			</view>
			<view class="item">
				<text class="row">电子邮件</text>
				<text>{{ userInfo.dept.email }}</text>
			</view>
			<view class="item" style="border: none;">
				<text class="row">经营场地地址</text>
				<text>{{ userInfo.dept.address }}</text>
			</view>
			<view class="item">
				<text class="row">经营场地面积</text>
				<text>{{ userInfo.dept.area + 'm²' }}</text>
			</view>
			<!-- <view class="item" style="border: none;">
				<text class="column">车辆</text>
				<view class="imgs">
					<u--image v-for="(item, index) in userInfo.dept.car" :src="baseUrl + item" width="80px" height="80px" @click="preview(userInfo.dept.car, index)"></u--image>
				</view>
			</view> -->
			<view class="item" style="border: none;">
				<text class="row">常温车</text>
				<view class="imgs">
					<u--image v-for="(item, index) in userInfo.dept.carNum1.split(',')" :src="$tool.oss(item)"
						width="80px" height="80px" @click="preview(userInfo.dept.carNum1.split(','), index)"></u--image>
				</view>

				<!-- <text>{{ userInfo.dept.car_num1 }}</text> -->
			</view>
			<view class="item">
				<text class="row">冷藏车</text>
				<view class="imgs">
					<u--image v-for="(item, index) in userInfo.dept.carNum2.split(',')" :src="$tool.oss(item)"
						width="80px" height="80px" @click="preview(userInfo.dept.carNum2.split(','), index)"></u--image>
				</view>

				<!-- <text>{{ userInfo.dept.car_num2 }}</text> -->
			</view>
			<view class="item" style="border: none;">
				<text class="column">食品保险</text>
				<view class="imgs">
					<u--image v-for="(item, index) in userInfo.dept.foodInsurance.split(',')" :src="$tool.oss(item)"
						width="80px" height="80px"
						@click="preview(userInfo.dept.foodInsurance.split(','), index)"></u--image>
				</view>
			</view>
			<view class="item">
				<text class="row">保险额度</text>
				<text>{{ userInfo.dept.sumInsured + '万' }}</text>
			</view>
			<view class="item">
				<text class="column">纳税证明</text>
				<view class="imgs">
					<u--image v-for="(item, index) in userInfo.dept.socialSecurity.split(',')" :src="$tool.oss(item)"
						width="80px" height="80px"
						@click="preview(userInfo.dept.socialSecurity.split(','), index)"></u--image>
				</view>
			</view>
		</view>
		<view class="btn">
			<u-button color="#0065FF" @click="updateOther" text="更新资质"></u-button>
		</view>
		<u-picker @click="uploadAptitude" @confirm="cateComfirm" @cancel="cateShow = false" :show="cateShow"
			keyName="name" :columns="cateOption"></u-picker>
	</view>
</template>

<script>
	// import config from '@/http/config.js';
	export default {
		data() {
			return {
				userInfo: {},
				baseUrl: '',
				cateShow: false,
				cateOption: []
			};
		},
		onLoad() {
			this.userInfo = this.$store.state.user
			this.getType();
			// this.getUserInfo();
		},
		methods: {
			async afterRead(event) {
				let lists = [].concat(event.file);
				let fileListLen = this.userInfo.dept.others.length;
				lists.map((item) => {
					this.userInfo.dept.others.push({
						...item,
						status: 'uploading',
						message: '上传中'
					});
				});
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url);
					if (result.code == 200) {
						let item = this.userInfo.dept.others[fileListLen];
						this.userInfo.dept.others.splice(
							fileListLen,
							1,
							Object.assign(item, {
								status: 'success',
								message: '',
								url: result.url[0]
							})
						);
						this.$forceUpdate();
					} else {
						uni.$u.toast(result.error);
						this.userInfo.dept.others.splice(fileListLen, 1);
					}
					fileListLen++;
				}
				uni.$u.toast('上传完成，请及时更新资质');
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: this.baseUrl + '/api/file/upload', // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						success: (res) => {
							resolve(JSON.parse(res.data));
						},
						file: (err) => {
							uni.$u.toast(err);
						}
					});
				});
			},
			async getType() {
				this.$api.getcateList({
					pageNum: -1,
					pageSize: -1
				}).then((response) => {
					this.cateOption = response.rows
				})
				return
				const {
					data
				} = await this.$api.getcateList();
				const cate = data.cate.filter((el) => !this.userInfo.dept.cate.includes(String(el.id)));
				this.cateOption = [cate];
			},
			oversize(e) {
				uni.$u.toast(`${e.file[0].name}超出最大限制10MB`);
			},
			async updateOther() {
				uni.showLoading();
				const otherStr = this.userInfo.dept.others
					.filter((el) => el.status == 'success')
					.map((el) => el.url)
					.join(',');
				if (!otherStr) {
					uni.$u.toast('暂无更新');
					return;
				}
				const {
					code,
					data,
					msg
				} = await this.$api.deitDept(this.userInfo.dept);
				uni.hideLoading();
				this.cateShow = false;
				if (code == 200) {
					this.userInfo.dept.others = this.userInfo.dept.others
						.filter((el) => el.status == 'success')
						.map((el) => {
							el = el.url;
							return el;
						});
					console.log(this.userInfo.dept.others);
					uni.$u.toast('资质更新成功，等待审核...');
				} else {
					uni.$u.toast(msg);
				}
			},
			async cateComfirm(e) {
				uni.showLoading();
				const {
					code,
					data,
					msg
				} = await this.$api.applyAdd({
					company_name: this.userInfo.dept.company_name,
					license: this.userInfo.dept.license,
					food: this.userInfo.dept.food,
					other: this.userInfo.dept.other,
					id_card_top: this.userInfo.dept.id_card_top,
					id_card_back: this.userInfo.dept.id_card_back,
					food_inspector: this.userInfo.dept.food_inspector,
					health: this.userInfo.dept.health.join(','),
					performance: this.userInfo.dept.performance.join(','),
					cate_id: e.value[0].id,
					nature: this.userInfo.dept.nature,
					aptitude: this.userInfo.dept.aptitude,
					name: this.userInfo.dept.name,
					phone: this.userInfo.dept.phone,
					email: this.userInfo.dept.email,
					address: this.userInfo.dept.address,
					area: this.userInfo.dept.area,
					// car: this.userInfo.dept.car.join(','),
					car_num1: this.userInfo.dept.car_num1,
					car_num2: this.userInfo.dept.car_num2,
					food_insurance: this.userInfo.dept.food_insurance.join(','),
					sum_insured: this.userInfo.dept.sum_insured,
					code: this.userInfo.dept.code,
					social_security: this.userInfo.dept.social_security.join(',')
				});
				uni.hideLoading();
				this.cateShow = false;
				if (code == 200) {
					uni.$u.toast('类目添加成功，等待审核...');
				} else {
					uni.$u.toast(msg);
				}
			},
			async getUserInfo() {
				const {
					data
				} = await this.$api.userInfo();
				this.userInfo = data.user.company;
				this.userInfo.dept.health = this.userInfo.dept.health.split(',');
				this.userInfo.dept.performance = this.userInfo.dept.performance.split(',');
				this.userInfo.dept.car_num2 = this.userInfo.dept.car_num2.split(',');
				this.userInfo.dept.car_num1 = this.userInfo.dept.car_num1.split(',');

				// this.userInfo.dept.car = this.userInfo.dept.car.split(',');
				this.userInfo.dept.food_insurance = this.userInfo.dept.food_insurance.split(',');
				this.userInfo.dept.social_security = this.userInfo.dept.social_security.split(',');
				if (!this.userInfo.dept.other) {
					this.userInfo.dept.others = [];
				} else {
					this.userInfo.dept.others = this.userInfo.dept.other.split(',').map((el) => {
						el = {
							url: this.baseUrl + el
						};
						return el;
					});
				}
				this.getType();
			},
			preview(e, i) {
				const arr = e.map((el) => {
					el = this.baseUrl + el;
					return el;
				});
				uni.previewImage({
					urls: arr,
					current: i
				});
			}
		}
	};
</script>

<style lang="scss" scoped>
	.container {
		background-color: #0065ff;

		.content {
			background-color: #fff;
			border-radius: 50rpx 50rpx 0 0;
			padding: 40rpx 20rpx 160rpx 20rpx;
			font-size: 26rpx;
			color: #333;

			.item {
				margin-bottom: 20rpx;
				padding: 20rpx 0;
				border-bottom: 1rpx dotted #ddd;

				>text {
					display: inline-block;
				}

				.row {
					margin-right: 20rpx;
				}

				.column {
					margin-bottom: 20rpx;
				}

				.imgs {
					display: flex;
					flex-wrap: wrap;

					.imgs_item {
						>text {
							margin-bottom: 6rpx;
							font-size: 24rpx;
							color: #999;
						}

						/deep/ .u-image {
							margin-right: 20rpx;
						}
					}
				}

				.tags {
					display: flex;
					align-items: center;
					flex-wrap: wrap;

					/deep/ .u-tag {
						margin-right: 10rpx !important;
						margin-bottom: 10rpx;
					}

					/deep/ .u-button {
						height: 46rpx;
						width: 100rpx;
						margin: 0 0 10rpx 0;
					}
				}
			}
		}

		.btn {
			position: fixed;
			bottom: 0;
			width: 100%;
			padding: 20rpx 20rpx 40rpx 20rpx;
			box-sizing: border-box;
			background-color: #fff;

			/deep/ .u-button {
				border-radius: 20rpx;
			}
		}
	}
</style>